package grsu.epam.airline.action;

import java.util.HashMap;
import java.util.Map;
import grsu.epam.airline.form.PlaneModifyForm;
import grsu.epam.airline.service.PlaneService;
import grsu.epam.airline.service.UserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.LookupDispatchAction;

public class PlanesAction extends LookupDispatchAction {
	String target;

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.ADMIN)) {
			target = ForwardActionTo.ADDPLANE;
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.ADMIN)) {
			PlaneModifyForm input = (PlaneModifyForm) form;
			PlaneService planeService = new PlaneService();
			if (input.getPlaneId() != 0) {
				planeService.deletePlane(input.getPlaneId());
				request.setAttribute("planes", planeService.getAllPlane());
				target = ForwardActionTo.PLANES;
			} else {
				ActionMessages errors = new ActionMessages();
				errors.add("choosePlane", new ActionMessage("error.plane"));
				request.setAttribute("planes", planeService.getAllPlane());
				target = ForwardActionTo.ERROR;
				saveErrors(request, errors);
			}
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.ADMIN)) {
			PlaneModifyForm input = (PlaneModifyForm) form;
			PlaneService planeService = new PlaneService();
			if (input.getPlaneId() != 0) {
				request.setAttribute("plane",
						planeService.getPlaneById(input.getPlaneId()));
				target = ForwardActionTo.UPDATEPLANE;
			} else {
				ActionMessages errors = new ActionMessages();
				errors.add("choosePlane", new ActionMessage("error.plane"));
				request.setAttribute("planes", planeService.getAllPlane());
				target = ForwardActionTo.ERROR;
				saveErrors(request, errors);
			}
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Map getKeyMethodMap() {
		Map map = new HashMap();
		map.put("plane.delete", "delete");
		map.put("plane.update", "update");
		map.put("plane.add", "add");
		return map;
	}
}
